package com.guan.system.manager;

import com.guan.common.core.constants.CacheConstants;
import com.guan.ojcommonredis.service.RedisService;
import com.guan.system.domain.exam.Exam;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class ExamCacheManager {

    @Resource
    private RedisService redisService;

    public void addCache2List(Exam exam){
        // 未完赛列表：key："e:l:uf"  ,  value : "examId"
        redisService.leftPushForList(getExamListKey(), exam.getExamId());
        // 将竞赛基本信息再存一份 String： key："e:d:examId" ，value:"基本信息（json）"
        redisService.setCacheObject(getExamDetailKey(exam.getExamId()), exam);
    }

    public void deleteCacheFromList(Long examId){
        redisService.removeForList(getExamListKey(), examId);
        redisService.deleteObject(getExamDetailKey(examId));
        redisService.deleteObject(getExamQuestionListKey(examId));
    }

    private String getExamListKey() {
       return CacheConstants.EXAM_LIST_UNFINISHED;
    }

    private String getExamDetailKey(Long examId){
        // key："e:d:examId"
        return CacheConstants.EXAM_DETAIL + examId;
    }

    private String getExamQuestionListKey(Long examId){
        return CacheConstants.EXAM_QUESTION_LIST + examId;
    }

}
